package bs.app.Client;

import bs.app.Constant.CConfig;
import bs.app.Constant.KeyConstant;
import bs.app.Main.CCanvas;
import bs.app.Main.CMidlet;
import bs.app.Position.CPosition;
import bs.app.Res.CRess;
import bs.app.android.Graphics;

public class DialogBox {
	public String title;
	public String content[];
	public int x = 0, y = 0;

	public Command leftSoftKey, rightSoftKey, centerSoftKey;
	public String url = "";
	public int DownLoadOrLink;

	public int cong = 0;
	public boolean dungchocharing = false;
	public String linkcharing = "";

	public DialogBox() {
		CRess.LoadDialog();
		x = (CCanvas.s_iWcanvas - CRess.dialog.getWidth()) / 2;
		y = (CCanvas.s_iHcanvas - CRess.dialog.getHeight()) / 2;
		DownLoadOrLink = 3;
	}

	public void setcharing(boolean charing, String link) {
		this.dungchocharing = charing;
		this.linkcharing = link;
	}

	public void setUrl(String url, int downloadorlink) {
		url = CCanvas.s_Instan.connect.URLEncode(url);
		this.url = url;
		this.DownLoadOrLink = downloadorlink;
	}

	public void initCommand(boolean LeftSoftKey, String LeftCaption,
			boolean CenterSoftKey, String CenterCaption, boolean RightSoftKey,
			String RightCaption) {
		resetSoftKey();
		if (LeftSoftKey == true) {
			leftSoftKey = new Command(LeftCaption, new ActionMenu() {
				public void perform() {
					if (DownLoadOrLink == 1 || DownLoadOrLink == 0) {
						try {
							// CCanvas.CurrentScreen=CCanvas.SaveStatePrevious;
							CCanvas.s_Instan.isbrun = true;
							CMidlet.instanMidlet.platformRequest(url);
							CMidlet.instanMidlet.exit();
						} catch (Exception e) {
							e.printStackTrace();
						}
						DownLoadOrLink = 3;
					}

					if (dungchocharing == true) {
						CCanvas.CurrentScreen = CCanvas.SaveStatePrevious;
						dungchocharing = false;
						CCanvas.s_Instan.connect.setURL(linkcharing);
						CCanvas.s_Instan.connect.RequestServerGetData();
					}
				}
			});
		}
		if (CenterSoftKey == true) {
			centerSoftKey = new Command(CenterCaption, new ActionMenu() {
				public void perform() {
					if (CCanvas.s_Instan.connect.landaumatketnoi == 2) {
						CMidlet.instanMidlet.exit();
						return;
					}
					CCanvas.CurrentScreen = CCanvas.SaveStatePrevious;

				}
			});
		}
		if (RightSoftKey == true) {
			rightSoftKey = new Command(RightCaption, new ActionMenu() {
				public void perform() {
					if (DownLoadOrLink == 1 || DownLoadOrLink == 0) // dung cho mo trinh duyet web
					{
						CCanvas.CurrentScreen = CCanvas.SaveStatePrevious;
						try {
							CMidlet.instanMidlet.platformRequest(url);
							// CCanvas.s_Instan.connect.links.addElement(url);
						} catch (Exception e) {
							e.printStackTrace();
						}

					}
					DownLoadOrLink = 3;
					if (dungchocharing == true) {
						CCanvas.CurrentScreen = CCanvas.SaveStatePrevious;
						dungchocharing = false;
					}
				}
			});
		}
	}

	public void resetSoftKey() {
		leftSoftKey = null;
		centerSoftKey = null;
		rightSoftKey = null;
	}

	public void initValue(String caution, String Content) {
		title = caution;
		content = CCanvas.s_Instan.smallfont.splitStrInLine(Content,
				CRess.dialog.getWidth() - 10);
	}

	public void Paint(Graphics g) {
		// draw Dialog Img
		if (CRess.dialog != null) {
			g.drawImage(CRess.dialog, x, y, 0);
		}
		// draw title
		CCanvas.s_Instan.smallfont.drawString(g, title, x + 10, y + 2, 0,
				0xffffff, 0xff0000, true);
		// draw content
		for (int i = 0; i < content.length; i++) {
			// xx is for drawing text in HCenter
			int xx = x
					+ (CRess.dialog.getWidth() - CCanvas.s_Instan.smallfont
							.stringWidth(content[i])) / 2 + 10;
			CCanvas.s_Instan.smallfont.drawString(g, content[i], xx, y
					+ (i * 15) + 20, 0, 0xffffff, 0xff0000, false);
		}
		PaintCommand(g);
	}

	public void PaintCommand(Graphics g) {
		if (CCanvas.s_Instan.connect.landaumatketnoi != 2) {
			if(CCanvas.s_iWcanvas > CRess.menubar.getWidth()){
				g.drawImage(CRess.menubar, 0, CCanvas.s_iHcanvas, CPosition.ANCHOR_LEFT_BOTTOM);
				for(int i = CRess.menubar.getWidth(); i < CCanvas.s_iWcanvas + CRess.footer_back.getWidth(); i+=CRess.footer_back.getWidth())
					g.drawImage(CRess.footer_back, i, CCanvas.s_iHcanvas, CPosition.ANCHOR_LEFT_BOTTOM);

			}else{
				g.drawImage(CRess.menubar, 0, CCanvas.s_iHcanvas, CPosition.ANCHOR_LEFT_BOTTOM);

			}
		}
		if (centerSoftKey != null) {
			CCanvas.mediumfont.drawString(
					g,
					centerSoftKey.caption,
					CCanvas.s_iWcanvas
							/ 2
							- CCanvas.mediumfont
									.stringWidth(centerSoftKey.caption) / 2,
					CCanvas.s_iHcanvas - CConfig.FONT_HEIGHT_COMMAND, 0,
					0xa1a6a1, 0x090b7a, true);
		}
		if (leftSoftKey != null) {
			CCanvas.mediumfont.drawString(g, leftSoftKey.caption, 5,
					CCanvas.s_iHcanvas - CConfig.FONT_HEIGHT_COMMAND, 0,
					0xa1a6a1, 0x090b7a, true);
		}
		if (rightSoftKey != null) {
			CCanvas.mediumfont.drawString(
					g,
					rightSoftKey.caption,
					CCanvas.s_iWcanvas
							- CCanvas.mediumfont
									.stringWidth(rightSoftKey.caption) - 1,
					 CCanvas.s_iHcanvas - 19, 0,
					0xa1a6a1, 0x090b7a, true);
		}
	}

	public void pointerPressed(int px, int py) {

		if (CCanvas.isPointerPressed) // User PointerPressed
		{
			if (Pointer.SelectSoftKey(px, py) == 0) {
				CCanvas.keyPressed[KeyConstant.KEY_LEFT_SOFTKEY] = true;
			} else if (Pointer.SelectSoftKey(px, py) == 3) {
				CCanvas.keyPressed[KeyConstant.KEY_RIGHT_SOFTKEY] = true;
			} else if (Pointer.SelectSoftKey(px, py) == 1
					|| Pointer.SelectSoftKey(px, py) == 2) {
				CCanvas.keyPressed[KeyConstant.KEY_FIRE] = true;
			}
		}
		CCanvas.resetPointerPress(); // User PointerReleased
	}

	public void UpdateKeyPress(boolean[] keyPressed, boolean[] keyHold,
			int currentAsccii[]) {
		update();
		if (keyPressed[KeyConstant.KEY_LEFT_SOFTKEY]) {
			if (leftSoftKey != null)
				leftSoftKey.action.perform();
			keyPressed[KeyConstant.KEY_LEFT_SOFTKEY] = false;
		} else if (keyPressed[KeyConstant.KEY_FIRE]) {
			if (centerSoftKey != null)
				centerSoftKey.action.perform();
			keyPressed[KeyConstant.KEY_FIRE] = false;
		} else if (keyPressed[KeyConstant.KEY_RIGHT_SOFTKEY]) {
			if (rightSoftKey != null)
				rightSoftKey.action.perform();
			keyPressed[KeyConstant.KEY_RIGHT_SOFTKEY] = false;
		}
		CCanvas.resetkey(); // User KeyReleased
	}

	public void update() {

	}
}
